﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Yoozoo.Gta.Common;
using Yoozoo.Modules.InnerCity.TouchCamera;

namespace com.yoozoo.gta.Extension.UGUI.ScreenOrientationEx
{
    [Serializable]
    public class InnerCityCameraComponentAdaptConfig
    {
        public float ZoomMaxLOD1;
        public float ZoomMaxLOD2;
        public float ZoomMaxLOD3;
        public float ZoomMaxLOD4;
        public float ZoomMaxLOD5;
        
        public QualityLod LowQualityLod;
        public QualityLod MediumQualityLod;
        public QualityLod HighQualityLod;
        public QualityLod VeryHighQualityLod;
        public QualityLod PerfectQualityLod;
        
        //重写Equals方法
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return false;
            }

            InnerCityCameraComponentAdaptConfig other = (InnerCityCameraComponentAdaptConfig)obj;
            
            if (ZoomMaxLOD1 != other.ZoomMaxLOD1
                || ZoomMaxLOD2 != other.ZoomMaxLOD2
                || ZoomMaxLOD3 != other.ZoomMaxLOD3
                || ZoomMaxLOD4 != other.ZoomMaxLOD4
                || ZoomMaxLOD5 != other.ZoomMaxLOD5)
            {
                return false;
            }
            
            if (LowQualityLod.Lod1 != other.LowQualityLod.Lod1
                || LowQualityLod.Lod2 != other.LowQualityLod.Lod2
                || LowQualityLod.Lod3 != other.LowQualityLod.Lod3
                || LowQualityLod.Lod4 != other.LowQualityLod.Lod4
                || LowQualityLod.Lod5 != other.LowQualityLod.Lod5
                || LowQualityLod.needHideShadow != other.LowQualityLod.needHideShadow)
            {
                return false;
            }
            
            if (MediumQualityLod.Lod1 != other.MediumQualityLod.Lod1
                || MediumQualityLod.Lod2 != other.MediumQualityLod.Lod2
                || MediumQualityLod.Lod3 != other.MediumQualityLod.Lod3
                || MediumQualityLod.Lod4 != other.MediumQualityLod.Lod4
                || MediumQualityLod.Lod5 != other.MediumQualityLod.Lod5
                || MediumQualityLod.needHideShadow != other.MediumQualityLod.needHideShadow)
            {
                return false;
            }
            
            if (HighQualityLod.Lod1 != other.HighQualityLod.Lod1
                || HighQualityLod.Lod2 != other.HighQualityLod.Lod2
                || HighQualityLod.Lod3 != other.HighQualityLod.Lod3
                || HighQualityLod.Lod4 != other.HighQualityLod.Lod4
                || HighQualityLod.Lod5 != other.HighQualityLod.Lod5
                || HighQualityLod.needHideShadow != other.HighQualityLod.needHideShadow)
            {
                return false;
            }
            
            if (VeryHighQualityLod.Lod1 != other.VeryHighQualityLod.Lod1
                || VeryHighQualityLod.Lod2 != other.VeryHighQualityLod.Lod2
                || VeryHighQualityLod.Lod3 != other.VeryHighQualityLod.Lod3
                || VeryHighQualityLod.Lod4 != other.VeryHighQualityLod.Lod4
                || VeryHighQualityLod.Lod5 != other.VeryHighQualityLod.Lod5
                || VeryHighQualityLod.needHideShadow != other.VeryHighQualityLod.needHideShadow)
            {
                return false;
            }
            
            if (PerfectQualityLod.Lod1 != other.PerfectQualityLod.Lod1
                || PerfectQualityLod.Lod2 != other.PerfectQualityLod.Lod2
                || PerfectQualityLod.Lod3 != other.PerfectQualityLod.Lod3
                || PerfectQualityLod.Lod4 != other.PerfectQualityLod.Lod4
                || PerfectQualityLod.Lod5 != other.PerfectQualityLod.Lod5
                || PerfectQualityLod.needHideShadow != other.PerfectQualityLod.needHideShadow)
            {
                return false;
            }

            return true;
        }
        //重写GetHashCode方法
        public override int GetHashCode()
        {
            int hash = 17;
            hash = hash * 23 + ZoomMaxLOD1.GetHashCode();
            hash = hash * 23 + ZoomMaxLOD2.GetHashCode();
            hash = hash * 23 + ZoomMaxLOD3.GetHashCode();
            hash = hash * 23 + ZoomMaxLOD4.GetHashCode();
            hash = hash * 23 + ZoomMaxLOD5.GetHashCode();
            hash = hash * 23 + LowQualityLod.GetHashCode();
            hash = hash * 23 + MediumQualityLod.GetHashCode();
            hash = hash * 23 + HighQualityLod.GetHashCode();
            hash = hash * 23 + VeryHighQualityLod.GetHashCode();
            hash = hash * 23 + PerfectQualityLod.GetHashCode();
            return hash;
        }
    }
    [DisallowMultipleComponent]
    public class InnerCityCameraComponentAdapt : AdaptBase<InnerCityCameraComponentAdaptConfig, InnerCityCameraComponent>
    {
        protected override void CopyProperty(InnerCityCameraComponentAdaptConfig config) {
            base.CopyProperty(config);
            config.ZoomMaxLOD1 = mComponent.ZoomMaxLOD1;
            config.ZoomMaxLOD2 = mComponent.ZoomMaxLOD2;
            config.ZoomMaxLOD3 = mComponent.ZoomMaxLOD3;
            config.ZoomMaxLOD4 = mComponent.ZoomMaxLOD4;
            config.ZoomMaxLOD5 = mComponent.ZoomMaxLOD5;
            config.LowQualityLod = mComponent.LowQualityLod;
            config.MediumQualityLod = mComponent.MediumQualityLod;
            config.HighQualityLod = mComponent.HighQualityLod;
            config.VeryHighQualityLod = mComponent.VeryHighQualityLod;
            config.PerfectQualityLod = mComponent.PerfectQualityLod;
        }

        protected override void ApplyConfig(InnerCityCameraComponentAdaptConfig config) {
            base.ApplyConfig(config);
            mComponent.ZoomMaxLOD1 = config.ZoomMaxLOD1;
            mComponent.ZoomMaxLOD2 = config.ZoomMaxLOD2;
            mComponent.ZoomMaxLOD3 = config.ZoomMaxLOD3;
            mComponent.ZoomMaxLOD4 = config.ZoomMaxLOD4;
            mComponent.ZoomMaxLOD5 = config.ZoomMaxLOD5;
            mComponent.LowQualityLod = config.LowQualityLod;
            mComponent.MediumQualityLod = config.MediumQualityLod;
            mComponent.HighQualityLod = config.HighQualityLod;
            mComponent.VeryHighQualityLod = config.VeryHighQualityLod;
            mComponent.PerfectQualityLod = config.PerfectQualityLod;
            
            // 更新组件和镜头
            StartCoroutine(updateCameraController());
        }
        
        IEnumerator updateCameraController()
        {
            // 这里要先设置下镜头位置，不然后面更新的范围会不准
            ResidentHandler.Inst.GetFunction("CityLuaFunction.resetCityCameraInitPos").Action();

            // 刷新相机控制器，更新可拖动范围和Zoom范围
            yield return new WaitForEndOfFrame();
            mComponent.UpdateCameraController();

            // 等待下一帧，再次设置下镜头位置，不然相机的z轴会不准
            yield return null;
            ResidentHandler.Inst.GetFunction("CityLuaFunction.resetCityCameraInitPos").Action();
        }
    }
}
